<?php

/**
 * Description of the relationship between a movie, the room 
 * and the times that it is playing at
 *
 * @author Scott McKay
 */
class Application_Model_ShowingAt extends Zend_Db_Table_Abstract {

	protected $_name = 'Showing_at';
	
	public function getRoom($movie_id,$time_id) {
		$sql = "SELECT Room_ID FROM Showing_at WHERE Movie_ID = :MID AND Time_ID = :TID";
 		$params = array(":MID" => $movie_id, ":TID" => $time_id);
 		return $this->getAdapter()->query($sql,$params)->fetchAll();
	}
	
	/**
	 * Function to add the relation between a movie, the room and time
	 * that it is playig at
	 */
	public function addShowAt($Movie_ID, $Room_ID, $Date) {
		$sql = "INSERT INTO Showing_at (ShowingAt_ID, Movie_ID, Room_ID, Date) VALUES (Null, :mid, :rid, :date)";
		$params = array(":mid" => $Movie_ID, ":rid" => $Room_ID, ":date" => $Date);
		$this->getAdapter()->query($sql,$params);
	}
	
	public function getShows() {
		$sql = "SELECT ShowingAt_ID, Title, Room_ID, Date FROM Showing_at sa, Movie m WHERE sa.Movie_ID = m.Movie_ID";
 		return $this->getAdapter()->query($sql)->fetchAll();
	}
	
	public function getShowsD($Date) {
		$sql = "SELECT ShowingAt_ID, Title, Room_ID, Date, Poster, m.Movie_ID FROM Showing_at sa, Movie m
				WHERE sa.Movie_ID = m.Movie_ID AND
				sa.Date LIKE :Date ORDER BY Date";
		$params = array(":Date" => $Date);
		
 		return $this->getAdapter()->query($sql, $params)->fetchAll();
	}

	public function deleteShow($id) {
		$sql = "DELETE FROM Showing_at WHERE ShowingAt_ID = ?";
		$params = array($id);
		$this->getAdapter()->query($sql,$params);
	}
	
	public function getDays() {
		$sql = "SELECT DISTINCT DATE_FORMAT( Date,  '%Y-%m-%d' ) AS Date FROM Showing_at WHERE Date <= '2012-06-09' ORDER BY Date";
 		return $this->getAdapter()->query($sql)->fetchAll();
	}
	
	
}

?>
